package com.yami.shop.rights.multishop.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.delivery.common.param.ExchangeRightsParam;
import com.yami.shop.delivery.common.service.DeliveryOrderService;
import com.yami.shop.rights.common.dto.ExchangeRightsDto;
import com.yami.shop.rights.common.model.ExchangeRecord;
import com.yami.shop.rights.common.service.ExchangeRecordService;
import com.yami.shop.security.multishop.util.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/p/exchangeRights")
@Tag(name = "兑换权益（商户端）")
@Slf4j
public class ExchangeRightsController {

    @Autowired
    private ExchangeRecordService exchangeRecordService;
    @Autowired
    private DeliveryOrderService deliveryOrderService;

    @GetMapping("/list")
    @Operation(summary = "兑换权益分页列表")
    public ServerResponseEntity<IPage<ExchangeRightsDto>> pageList(ExchangeRightsDto vo, PageParam<ExchangeRightsDto> page) {
        vo.setShopId(SecurityUtils.getShopUser().getShopId());
        IPage<ExchangeRightsDto> pageData = exchangeRecordService.pageList(page, vo);
        return ServerResponseEntity.success(pageData);
    }


    @GetMapping("/unDeliveryExchangeExcel")
    @Operation(summary =  "导出待发货订单", description = "导出待发货订单")
    public void unDeliveryExchangeExcel(@ParameterObject ExchangeRightsDto exchangeRightsDto, HttpServletResponse response) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        exchangeRightsDto.setShopId(shopId);
        exchangeRecordService.unDeliveryExchangeExcel(exchangeRightsDto, response);
    }

    @Operation(summary =  "导入待发货文件", description = "导入待发货文件")
    @PostMapping("/exportExchangeExcel")
    @ResponseBody
    public ServerResponseEntity<String> exportExchangeExcel(@RequestParam("orderExcelFile") MultipartFile orderExcelFile) throws Exception {
        if (Objects.isNull(orderExcelFile)) {
            throw new YamiShopBindException("yami.network.busy");
        }
        Long shopId = SecurityUtils.getShopUser().getShopId();
        List<ExchangeRightsDto> exchangeList = new ArrayList<>();
        String errorMsg = exchangeRecordService.exportExchangeExcel(orderExcelFile, shopId, exchangeList);
        if (Objects.nonNull(errorMsg)) {
            return ServerResponseEntity.success(errorMsg);
        }
        //无错误再发货
        for (ExchangeRightsDto exchangeRightsDto : exchangeList) {
            //待发货的处理
            if("2".equals(exchangeRightsDto.getStatus())){
                ExchangeRightsParam exchangeRightsParam = BeanUtil.copyProperties(exchangeRightsDto, ExchangeRightsParam.class);
                deliveryOrderService.saveDeliveriesInfo(exchangeRightsParam, shopId);
                //修改为发货状态
                ExchangeRecord exchengeRecord = new ExchangeRecord();
                exchengeRecord.setId(exchangeRightsDto.getId());
                exchengeRecord.setStatus("3");
                exchengeRecord.setCity(exchangeRightsDto.getCity());
                exchengeRecord.setProvince(exchengeRecord.getProvince());
                exchengeRecord.setDvyType(exchangeRightsDto.getDvyType());
                exchangeRecordService.updateById(exchengeRecord);
            }
        }
        return ServerResponseEntity.success("导入成功！");
    }

}
